#ifndef TIMER_H
#define TIMER_H

#include <sys/time.h>

/* A simple wrapper around the clock() timer */
typedef struct{
    struct timeval clock_holder;
    struct timeval duration;
}Timer;

void
initialize_timer (Timer * t)
{
    t->clock_holder.tv_sec = 0;
    t->clock_holder.tv_usec = 0;
    t->duration.tv_sec = 0;
    t->duration.tv_usec = 0;
}

void
start_timer(Timer * t)
{
    gettimeofday (&t->clock_holder, NULL);
    //printf("%d %d\n",t->clock_holder.tv_sec, t->clock_holder.tv_usec);
}

void
stop_timer(Timer * t)
{
    struct timeval end_tv;
    gettimeofday (&end_tv, NULL);
    //printf("%d %d\n",end_tv.tv_sec, end_tv.tv_usec);
    t->duration.tv_sec += (end_tv.tv_sec - t->clock_holder.tv_sec);
    t->duration.tv_usec += (end_tv.tv_usec - t->clock_holder.tv_usec);
    //printf("%d %d\n",t->duration.tv_sec, t->duration.tv_usec);
}

double
timer_duration(const Timer t)
{
    //printf("%f\n", t.duration.tv_sec + 1.0e-6 * (double)t.duration.tv_usec);
    return t.duration.tv_sec + 1.0e-6 * (double)t.duration.tv_usec;
}

#endif
